<?php

# http://projects:8081/cdapp-2kt-api/api/seat_hours_get.php

include_once '_portal.php';

// const PARAM_NAME_USERS = 'ATCOSUsers';
// const PARAM_NAME_USERS_GROUPED = 'ATCOSUsersGrouped';

forbidden_acc();

const PARAM_NAME_USERS         = 'ATCOS_USERS_CONTENT';
const PARAM_NAME_USERS_GROUPED = 'ATCOS_USERS_CONTENT_GROUPED';

$users         = param(PARAM_NAME_USERS);
$users_grouped = param(PARAM_NAME_USERS_GROUPED);

// 
// 将新版本人员信息转换为就版本
// @2024/12/01
//
// users:
// 
// old versions:
// [{"name": "陈华宽", "number": 20070023, "office": "管制一室"}]
// 
// new versions":
// [{"orgId":4,"fullName":"闫啸","code":"20000016","orgName":"终端管制室"}]
// 
// users_grouped:
// old versions:
// [{"label": "管制一室", "value": "12", "children": [{"label": "陈华宽", "value": 20070023}]}]
// 
// new versions":
// [{"orgName":"终端管制室","orgId":"4","users":[{"fullName":"闫啸","code":"20000016"}]}]
//

if (isset($users) && is_array($users) && count($users) > 0) {
  if (! isset($users[0]['name'])) {
    for ($i = 0; $i < count($users); $i++) {
      $users[$i]['name']   = $users[$i]['fullName'];
      $users[$i]['number'] = $users[$i]['code'];
      $users[$i]['office'] = str_replace('终端', '', $users[$i]['orgName']);
    }
  }
}

// users_grouped
if (isset($users_grouped) && is_array($users_grouped) && count($users_grouped) > 0) {
  for ($i = 0; $i < count($users_grouped); $i++) {
    if (! isset($users_grouped[$i]['label'])) {
      $users_grouped[$i]['label']    = str_replace('终端', '', $users_grouped[$i]['orgName']);
      $users_grouped[$i]['value']    = $users_grouped[$i]['orgId'];
      $users_grouped[$i]['children'] = [];
      if (is_array($users_grouped[$i]['children'])) {
        for ($j = 0; $j < count($users_grouped[$i]['users']); $j++) {
          $users_grouped[$i]['users'][$j]['label'] = $users_grouped[$i]['users'][$j]['fullName'];
          $users_grouped[$i]['users'][$j]['value'] = $users_grouped[$i]['users'][$j]['code'];
          $users_grouped[$i]['children'][]         = $users_grouped[$i]['users'][$j];
        }
      }
    }
  }
}

$start = isset($_GET['start']) ? urldecode($_GET['start']) : (isset($post->start) ? $post->start : date('Y-m-d H:i:s', time() - 24 * 3600));

$end = isset($_GET['end']) ? urldecode($_GET['end']) : (isset($post->end) ? $post->end : date('Y-m-d H:i:s'));

$with_holiday = isset($_GET['holiday']) ? intval($_GET['holiday']) : (isset($post->holiday) ? $post->holiday : 0);

$with_user = isset($_GET['user']) ? intval($_GET['user']) : (isset($post->user) ? $post->user : 0);

$sql = "SELECT * FROM `2kt_seat_hours` WHERE `startTime` <= '{$end}' AND (`endTime` > '{$start}' OR ISNULL(`endTime`)) ORDER BY `startTime` ASC";

$result = exec_sql($sql);
$result = $result ? $result : []; // 避免查询不到结果返回null或false，而应该返回空数组

$output = [
  'start'        => $start,
  'end'          => $end,
  'count'        => count($result),
  'content'      => $result,
  'users'        => $users,
  'usersGrouped' => $users_grouped,
  // 'sql' => $sql,
];

$with_holiday ? ($output['holidays'] = param('MISC_HOLIDAYS')) : null;
$with_user ? ($output['users'] = param('ATCOS_USERS_CONTENT')) : null;
$with_user ? ($output['usersGrouped'] = param('ATCOS_USERS_CONTENT_GROUPED')) : null;

output($output);
